package com.ckk.eurekaclient.controller;

import com.ckk.eurekaclient.constants.MqQueue;
import com.ckk.eurekaclient.enums.SyncTypeEnum;
import com.ckk.eurekaclient.pojo.Admin;
import com.ckk.eurekaclient.pojo.MarkerPo;
import com.ckk.eurekaclient.pojo.RespBean;
import com.ckk.eurekaclient.service.IAdminService;
import com.ckk.eurekaclient.service.mq.MqSendService;
import com.ckk.eurekaclient.vo.req.AdminLoginParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @author zhujunjun
 */
@Api(tags = "LoginController")
@Slf4j
@RestController
public class LoginController {

    @Autowired
    private IAdminService adminService;

    @Autowired
    private MqSendService mqSendService;

    @ApiOperation(value = "登录之后返回token")
    @PostMapping("/login")
    public RespBean login(@RequestBody AdminLoginParam adminLoginParam, HttpServletRequest request) {
        RespBean login = adminService.login(adminLoginParam.getUsername(), adminLoginParam.getPassword(),
                adminLoginParam.getCode(), request);
        if (!"200".equals(login.getCode())) {
            return login;
        }
        SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        return login;
    }

    @ApiOperation(value = "获取当前登录用户的信息")
    @GetMapping("/rabbitTest")
    public Admin getAdminInfo() {
//        if (null == principal) {
//            return null;
//        }
//        String username = principal.getName();
//        Admin admin = adminService.getAdminByUserName(username);
//        admin.setPassword(null);
        log.info("发送信息");
        MarkerPo markerPo = new MarkerPo();
        markerPo.setMItemId(1000);
        markerPo.setMLayer(1000);
        mqSendService.send(MqQueue.EXCHANGE_LOCAL_STACK_INFO, SyncTypeEnum.UPDATE.getCode(), markerPo);
        return null;
    }

    @ApiOperation(value = "退出登录")
    @PostMapping("/logout")
    public RespBean logout() {
        return RespBean.success("注销成功！");
    }

}
